'''
@Author: your name
@Date: 2019-09-25 09:48:29
@LastEditTime: 2020-03-04 17:08:50
@LastEditors: Please set LastEditors
@Description: In User Settings Edit
@FilePath: \IntelligentElderlyCare\server\register\buss_pub\person_org_manage_register.py
'''

from server.pao_python.pao.remote import UnauthorizedError
from ...service.security_module import SecurityModule, FunctionName, PermissionName
from ...service.buss_pub.person_org_manage import PersonOrgManageService
from ...pao_python.pao.service.data.mongo_db import N

'''
版权：Copyright (c) 2019 China

创建日期：Tuesday September 17th 2019
创建者：ymq(ymq) - <<email>>

修改日期: Tuesday, 17th September 2019 8:51:29 pm
修改者: ymq(ymq) - <<email>>

说明
 1、
'''


def register(jsonrpc, db_addr, db_port, db_name, db_user, db_pwd, inital_password, session):
    person_org_manage_service = PersonOrgManageService(
        db_addr, db_port, db_name, db_user, db_pwd, inital_password, session)
    security_service = SecurityModule(
        db_addr, db_port, db_name, db_user, db_pwd, inital_password, session)

    @jsonrpc.method('IPersonOrgManageService.get_current_user_info')
    def __get_current_user_info():
        '''获取当前登录用户信息'''
        res = person_org_manage_service.get_current_user_info()
        return res

############################################################################################

    @jsonrpc.method('IPersonOrgManageService.get_elder_list_all')
    def __get_user_list_area(condition, page=None, count=None):
        '''获取长者列表'''
        permission_data = security_service.judge_permission_query(
            FunctionName.elder_manage_all, PermissionName.query)
        res = person_org_manage_service.get_user_elder_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_wait_checkin_elder_list')
    def __get_user_list_area(condition={}, page=None, count=None):
        '''获取等待入住的长者列表'''
        permission_data = security_service.judge_permission_query(
            FunctionName.elder_manage_all, PermissionName.query)
        res = person_org_manage_service.get_wait_checkin_elder_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_elder_list_by_org')
    def __get_elder_list_by_org(condition, page=None, count=None):
        '''获取当前机构长者列表'''
        permission_data = security_service.judge_permission_query(
            FunctionName.org_elder_manage_all, PermissionName.query)
        res = person_org_manage_service.get_elder_list_by_org(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_elder_list_look')
    def __get_user_list_area(condition, page=None, count=None):
        '''查询长者资料'''
        permission_data = security_service.judge_permission_query(
            FunctionName.elder_info_look, PermissionName.query)
        res = person_org_manage_service.get_user_elder_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.update_elder_info')
    def __update_elder_info_all(condition):
        '''长者管理 修改/新增'''
        if 'id' in list(condition.keys()):
            __p = security_service.judge_permission_other(
                FunctionName.elder_manage_all, PermissionName.edit)
        else:
            __p = security_service.judge_permission_other(
                FunctionName.elder_manage_all, PermissionName.add)
        res = person_org_manage_service.update_elder_info(condition)
        return res['res']

    @jsonrpc.method('IPersonOrgManageService.delete_elder_info')
    def __delete_elder_info_all(condition):
        '''长者管理 删除'''
        __P = security_service.judge_permission_other(
            FunctionName.elder_manage_all, PermissionName.delete)
        res = person_org_manage_service.delete_elder_info(condition)
        return res
###################################################################

    @jsonrpc.method('IPersonOrgManageService.get_organization_list_all')
    def __get_organization_list_all(condition, page=None, count=None):
        '''获取组织机构列表'''
        permission_data = security_service.judge_permission_query(
            FunctionName.organization_all, PermissionName.query)
        res = person_org_manage_service.get_organization_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.update_organization_info')
    def __update_organization_info(condition):
        '''组织机构 修改/新增'''
        if 'id' in list(condition.keys()):
            __P = security_service.judge_permission_other(
                FunctionName.organization_all, PermissionName.edit)
        else:
            __P = security_service.judge_permission_other(
                FunctionName.organization_all, PermissionName.add)
        res = person_org_manage_service.update_elder_info(condition)
        return res['res']

    @jsonrpc.method('IPersonOrgManageService.delete_organization_info')
    def __delete_elder_info_all(condition):
        '''组织机构 删除'''
        __P = security_service.judge_permission_other(
            FunctionName.organization_all, PermissionName.delete)
        res = person_org_manage_service.delete_elder_info(condition)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_organization_list_look')
    def __get_organization_list_look(condition, page=None, count=None):
        '''查询 组织机构'''
        permission_data = security_service.judge_permission_query(
            FunctionName.organization_look, PermissionName.query)
        res = person_org_manage_service.get_organization_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_worker_list_all')
    def __get_user_list_area(condition, page=None, count=None):
        '''工作人员管理 查询'''
        permission_data = security_service.judge_permission_query(
            FunctionName.worker_manage_all, PermissionName.query)
        res = person_org_manage_service.get_user_worker_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.update_personnel_info')
    def __update_elder_info_all(condition):
        '''人员管理 修改/新增'''
        if 'id' in list(condition.keys()):
            __p = security_service.judge_permission_other(
                FunctionName.personnel_manage_all, PermissionName.edit)
        else:
            __p = security_service.judge_permission_other(
                FunctionName.personnel_manage_all, PermissionName.add)
        res = person_org_manage_service.update_elder_info(condition)
        return res['res']

    @jsonrpc.method('IPersonOrgManageService.delete_personnel_info')
    def __delete_elder_info_all(condition):
        '''工作人员管理 删除'''
        __p = security_service.judge_permission_other(
            FunctionName.personnel_manage_all, PermissionName.delete)
        res = person_org_manage_service.delete_elder_info(condition)
        return res

    @jsonrpc.method('IPersonOrgManageService.delete_organization')
    def __delete_organization(condition):
        '''组织机构 删除'''
        __p = security_service.judge_permission_other(
            FunctionName.organization_all, PermissionName.delete)
        res = person_org_manage_service.delete_organization(condition)
        return res

    @jsonrpc.method('IPersonOrgManageService.update_worker_info')
    def __update_elder_info_all(condition):
        '''工作人员管理 修改/新增'''
        if 'id' in list(condition.keys()):
            __p = security_service.judge_permission_other(
                FunctionName.worker_manage_all, PermissionName.edit)
        else:
            __p = security_service.judge_permission_other(
                FunctionName.worker_manage_all, PermissionName.add)
        res = person_org_manage_service.update_elder_info(condition)
        return res['res']

    @jsonrpc.method('IPersonOrgManageService.update_organization')
    def __update_organization(condition):
        '''组织机构 修改/新增'''
        if 'id' in list(condition.keys()):
            __p = security_service.judge_permission_other(
                FunctionName.organization_all, PermissionName.edit)
        else:
            __p = security_service.judge_permission_other(
                FunctionName.organization_all, PermissionName.add)
        res = person_org_manage_service.update_organization(condition)
        return res

    @jsonrpc.method('IPersonOrgManageService.delete_worker_info')
    def __delete_elder_info_all(condition):
        '''工作人员管理 删除'''
        __p = security_service.judge_permission_other(
            FunctionName.worker_manage_all, PermissionName.delete)
        res = person_org_manage_service.delete_elder_info(condition)
        return res

    # @jsonrpc.method('IPersonOrgManageService.get_org_worker_list_all')
    # def __get_user_list_area(condition, page=None, count=None):
    #     '''机构人员管理 查询'''
    #     permission_condtion = {'user_type': ['工作人员']}
    #     permission_data = security_service.judge_permission_query(
    #         FunctionName.org_worker_manage_all, PermissionName.query)
    #     res = person_org_manage_service.get_user_all_list(permission_data,
    #                                                       permission_condtion, condition, page=None, count=None)
    #     return res

    # @jsonrpc.method('IPersonOrgManageService.update_org_worker_info')
    # def __update_elder_info_all(condition):
    #     '''机构人员管理 修改/新增'''
    #     if 'id' in list(condition.keys()):
    #         __p = security_service.judge_permission_other(
    #             FunctionName.org_worker_manage_all, PermissionName.edit)
    #     else:
    #         __p = security_service.judge_permission_other(
    #             FunctionName.org_worker_manage_all, PermissionName.add)
    #     res = person_org_manage_service.update_elder_info(condition)
    #     return res

    # @jsonrpc.method('IPersonOrgManageService.delete_org_worker_info')
    # def __delete_elder_info_all(condition):
    #     '''机构人员管理 删除'''
    #     __p = security_service.judge_permission_other(
    #         FunctionName.org_worker_manage_all, PermissionName.delete)
    #     res = person_org_manage_service.delete_elder_info(condition)
    #     return res

#############################人员组织列表下拉框查询接口，不做功能权限判断，仅做范围控制##############################################
    @jsonrpc.method('IPersonOrgManageService.get_organization_list')
    def __get_organization_list(condition, page=None, count=None):
        '''查询 组织机构'''
        permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_organization_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_organization_tree_list')
    def __get_organization_tree_list(condition, page=None, count=None):
        '''查询 组织机构树形结构'''
        permission_data = security_service.org_current()
        res = person_org_manage_service.get_organization_tree_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_organization_xfy_tree_list')
    def __get_organization_xfy_tree_list(condition, page=None, count=None):
        '''查询幸福院组织机构树形结构'''
        permission_data = security_service.org_current()
        res = person_org_manage_service.get_organization_xfy_tree_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_personnel_elder')
    def __get_elder_list(condition, page=None, count=None):
        '''查询长者'''
        permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_user_elder_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_elder_list')
    def __get_elder_list(condition, page=None, count=None):
        '''查询长者，单纯长者的信息，不关联其他表'''
        permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_elder_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_personnel_worker')
    def __get_elder_list(condition, page=None, count=None):
        '''查询工作人员'''
        permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_user_worker_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_personnel_list_all')
    def __get_elder_list(condition, page=None, count=None):
        '''查询全部人员（机构范围内的工作人员和全部平台游客）'''
        permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_user_all_list(
            permission_data, condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_personnel_classification_list')
    def __get_personnel_classification_list(condition, page=None, count=None):
        '''查询人员类别'''
        # permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_personnel_classification_list(
            N(), condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.update_personnel_classification')
    def __update_personnel_classification(condition):
        '''人员类别修改/新增'''
        if 'id' in list(condition.keys()):
            __p = security_service.judge_permission_other(
                FunctionName.personnel_classification_all, PermissionName.edit)
        else:
            __p = security_service.judge_permission_other(
                FunctionName.personnel_classification_all, PermissionName.add)
        res = person_org_manage_service.update_personnel_classification(
            condition)
        return res

    @jsonrpc.method('IPersonOrgManageService.delete_personnel_classification')
    def __delete_personnel_classification(condition):
        '''长者管理 删除'''
        __P = security_service.judge_permission_other(
            FunctionName.personnel_classification_all, PermissionName.delete)
        res = person_org_manage_service.delete_personnel_classification(
            condition)
        return res

    @jsonrpc.method('IPersonOrgManageService.import_excel_manage')
    def __import_excel_manage(condition):
        '''从excel导入数据'''
        __P = security_service.judge_permission_other(
            FunctionName.import_excel_manage, PermissionName.add)
        res = person_org_manage_service.import_excel_manage(condition)
        return res

#############################人员组织列表下拉框查询接口，不做功能权限、范围判断##############################################
    @jsonrpc.method('IPersonOrgManageService.get_all_organization_list')
    def __get_all_organization_list(condition, page=None, count=None):
        '''查询 全部组织机构'''
        res = person_org_manage_service.get_organization_list(
            N(), condition, page, count)
        return res

# 插入权限
# @jsonrpc.method('IPersonOrgManageService.addPersiom')
# def __addPersiom(conditon):
#     for x in conditon:

    @jsonrpc.method('IPersonOrgManageService.get_current_user_reservation_registration')
    def __get_current_user_reservation_registration(condition, page=None, count=None):
        '''获取当前用户机构预约记录'''
        res = person_org_manage_service.get_current_user_reservation_registration(condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_current_user_activity_participate')
    def __get_current_user_activity_participate(condition, page=None, count=None):
        '''获取当前用户活动报名记录'''
        res = person_org_manage_service.get_current_user_activity_participate(condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_current_user_allowance_manage')
    def __get_current_user_allowance_manage(condition, page=None, count=None):
        '''获取当前用户补贴申请记录'''
        res = person_org_manage_service.get_current_user_allowance_manage(condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_current_user_charitable_donate')
    def __get_current_user_charitable_donate(condition, page=None, count=None):
        '''获取当前用户慈善申请记录'''
        res = person_org_manage_service.get_current_user_charitable_donate(condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_current_user_acceptance')
    def __get_current_user_acceptance(condition, page=None, count=None):
        '''获取当前用户受理记录'''
        permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_current_user_acceptance(permission_data, 
            condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.get_current_user_message_notice')
    def __get_current_user_message_notice(condition, page=None, count=None):
        '''获取当前用户消息提醒'''
        permission_data = security_service.org_all_subordinate()
        res = person_org_manage_service.get_current_user_message_notice(permission_data, 
            condition, page, count)
        return res

    @jsonrpc.method('IPersonOrgManageService.check_permission')
    def __check_permission(condition):
        '''判断活动和资讯的权限'''
        res = person_org_manage_service.check_permission(
            condition)
        return res
